<template>
  <div class="app-container">
    <!-- 功能区 -->
    <transition name="formFade">
      <el-card class="operate-container" shadow="never" v-if="showSearch">
        <el-form :inline="true" :model="listQuery" @submit.native.prevent>
          <el-form-item label="是否暂停项目" class="form-item">
            <el-select placeholder="请选择" style="width:100%;" clearable v-model="listQuery.needStop"
                       class="form-select" :popper-append-to-body="false" popper-class="select-popper">
              <el-option :value="0" label="不暂停"></el-option>
              <el-option :value="1" label="暂停"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="整改状态" class="form-item">
            <el-select placeholder="请选择" style="width:100%;" clearable v-model="listQuery.status"
                       class="form-select" :popper-append-to-body="false" popper-class="select-popper">
              <el-option :value="0" label="下达整改单"></el-option>
              <el-option :value="1" label="整改后提交"></el-option>
              <el-option :value="2" label="整改通过"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item class="form-item">
            <el-button type="primary" size="mini" icon="el-icon-search" @click="getList">查询</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </transition>
    <div class="table_content">
      <headerSearch addTitle="下达整改单"
                    @handleAdd="handleAdd"
                    @creRefresh="getList"
                    @isSearch="showSearch = !showSearch"></headerSearch>
      <!--表格渲染-->
      <el-table ref="table" v-loading="loading" :data="list" style="width: 100%;" border
                :header-cell-style="{background: '#F8F8F9',padding: '6px 0 !important',color: '#515a6e'}">
        <af-table-column prop="projectName" fixed="left" label="项目名称" />
        <af-table-column prop="description" label="整改描述" />
        <!--<el-table-column prop="projectType" label="工程类型">
          <template slot-scope="scope">
            {{{'0':'工程项目','1':'政府采购项目','2':'资产资源招标'}[scope.row.projectType]}}
          </template>
        </el-table-column>-->
        <el-table-column prop="status" label="整改状态">
          <template slot-scope="scope">
            {{{'0':'下达整改单','1':'整改后提交','2':'整改通过'}[scope.row.status]}}
          </template>
        </el-table-column>
        <el-table-column prop="needStop" label="是否暂停项目">
          <template slot-scope="scope">
            {{{'0':'不暂停','1':'暂停'}[scope.row.needStop]}}
          </template>
        </el-table-column>
        <af-table-column prop="reason" label="整改原因" />
        <el-table-column prop="rectifyTime" label="整改单下达时间" />
        <el-table-column fixed="right" label="操作" min-width="80">
          <template slot-scope="scope">
            <el-button class="btn" type="text" size="small"
                       @click="rectifySubmit(scope.$index,scope.row)">提交整改</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <pagination v-show="total>0" :total="total"
                  :page.sync="listQuery.pageNum"
                  :limit.sync="listQuery.pageSize"
                  @pagination="getList" />
    </div>

      <!--表单添加-->
      <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
        <el-form :model="editForm" :rules="editFormRules" ref="editForm" label-width="130px">
          <el-form-item label="项目名称:" prop="projectID" class="form-item">
            <el-select placeholder="请选择" style="width:100%;" clearable v-model="editForm.projectID"
                       class="form-select" :popper-append-to-body="false" popper-class="select-popper">
              <el-option v-for="item in projectList" :value="item.id" :label="item.projectName"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="是否暂停项目:" prop="needStop" class="form-item">
            <el-select placeholder="请选择" style="width:100%;" clearable v-model="editForm.needStop"
                       class="form-select" :popper-append-to-body="false" popper-class="select-popper">
              <el-option :value="0" label="不暂停"></el-option>
              <el-option :value="1" label="暂停"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="整改原因:" prop="reason	" class="form-item">
            <el-input v-model="editForm.reason" class="filter-item" placeholder="整改原因"
                      type="textarea" :autosize="{ minRows: 1, maxRows: 4}"/>
          </el-form-item>
          <el-form-item label="整改描述:" prop="desc	" class="form-item">
            <el-input v-model="editForm.desc" class="filter-item" placeholder="整改描述"
                      type="textarea" :autosize="{ minRows: 1, maxRows: 4}"/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button size="mini" @click.native="dialogFormVisible=false">取消</el-button>
          <el-button type="primary" size="mini" @click="createData">
            {{dialogStatus == 'create'?'添加':'保存'}}
          </el-button>
        </div>
      </el-dialog>
  </div>
</template>

<script>
  import { doPost, doGet, doFile, orglistChild } from '@/api/index'
  import Pagination from '@/components/Pagination/index'
  import headerSearch from '@/components/headerSearch/headerSearch'

  export default {
    name: "punchLists",
    components: {Pagination, headerSearch},
    data() {
      return {
        loading: false,
        list: [],
        total: 0,
        listQuery: {
          pageNum: 1,
          pageSize: 20,
          needStop: '',
          status: '',
          projectType: 1
        },
        showSearch: true,
        dialogStatus: '',
        textMap:{
          update: '编辑',
          create: '新增'
        },
        dialogFormVisible: false,
        editForm: {
          projectType: '',
          projectID: '',
          needStop: '',
          reason: '',
          desc: '',
        },
        editFormRules: {
        },
        projectList: []
      }
    },
    created() {
      this.getProjectList()
    },
    methods: {
      getProjectList(){
        doPost('purchase/page').then(res => {
          this.projectList = res.data.list
        }).catch(error => {
          this.projectList = []
        })
      },
      getList() {
        this.loading = true
        doPost('rectify/page', this.listQuery).then(res => {
          this.loading = false
          this.list = res.data.list
          this.total = res.data.total
        }).catch(error => {
          this.list = []
          this.total = 0
          this.loading = false
        })
      },
      handleAdd() {
        this.dialogStatus = 'create'
        this.dialogFormVisible = true
        this.editForm = {
          projectType: 1,
          projectID: '',
          needStop: '',
          reason: '',
          desc: '',
        }
        this.$nextTick(() => {
          this.$refs['editForm'].clearValidate()
        })
      },
      //提交表单
      createData() {
        this.$refs['editForm'].validate(valid => {
          if (valid) {
            doPost(`rectify/${this.dialogStatus == 'create' ? 'add' : 'update'}`,this.editForm).then(res => {
              if (res.code == 200){
                this.dialogFormVisible = false
                this.getList()
                this.$message.success({
                  message: this.dialogStatus == 'create'?'提交成功':'保存成功',
                  type: 'success'
                })
              } else {
                this.$message.error(res.message)
              }
            }).catch(error => {
              this.$message.error(error.message)
            })
          }
        })
      },
      //提交整改
      rectifySubmit(index, row){}
    }
  }
</script>

<style scoped>

</style>
